package com.saishi.servlet;

import com.saishi.dao.BookDao;
import com.saishi.dao.BooksQuestionDao;
import com.saishi.dao.impl.BookDaoImpl;
import com.saishi.dao.impl.BooksQuestionDaoImpl;
import com.saishi.entity.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "QuestionBankServlet", value = "/user/question/*")
public class QuestionBankServlet extends BaseOneServlet {
    BookDao bookDao = new BookDaoImpl();
    BooksQuestionDao booksQuestionDao = new BooksQuestionDaoImpl();

    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String sbookid = request.getParameter("bookid");
        int bookid = 0;
        if (sbookid != null && !"".equals(sbookid))
            bookid = Integer.parseInt(sbookid);
        int delete = bookDao.delete(bookid);
        Long count = booksQuestionDao.count(bookid);
        int i = 0;
        if (count != 0)
            i = booksQuestionDao.deleteBook(bookid);
//        System.out.println("deleteBook"+delete+":"+i);
        response.sendRedirect("/user/question/");
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String sbookid = request.getParameter("bookid");
        String bookname = request.getParameter("bookname");
        int bookid = 0;
        if (sbookid != null && !"".equals(sbookid))
            bookid = Integer.parseInt(sbookid);
        int update = bookDao.update(new Book(bookid, bookname));
//        System.out.println(update);
        response.sendRedirect("/user/question/");
    }

    protected void databank(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        new DatabankServlet().doGet(request, response);
    }

    protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getParameter("");
        request.getRequestDispatcher("/WEB-INF/user/edit.jsp").forward(request, response);
    }

    protected void tolead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/user/tolead.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Book> books = null;
        try {
            books = bookDao.allQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println(books);
        request.setAttribute("books", books);
        request.getRequestDispatcher("/WEB-INF/user/manage.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
